﻿package core.drawing
{
    import core.enum.*;
    import core.image.*;
    import flash.events.*;
    import flash.geom.*;

    final public class MarqueeTool extends SelectionTool
    {
        var rect:Rectangle;

        public function MarqueeTool(param1:Tuyitu)
        {
            super(param1, ToolType.Marquee);
            return;
        }// end function

        override public function MouseDown(event:MouseEvent) : void
        {
            this.UpdateReferences();
            if (type.attitude.method != "move")
            {
                history.Prepare(options.action);
                ed.cursor.freeze = true;
                canvas.back.removeEventListener(MouseEvent.MOUSE_MOVE, MouseMove, false);
                ed.addEventListener(MouseEvent.MOUSE_MOVE, this.MouseDrag, false, 1, true);
                ed.addEventListener(MouseEvent.MOUSE_UP, this.MouseUp, false, 1, true);
                if (selection != null && !event.shiftKey && !event.ctrlKey)
                {
                    selection.Reset();
                    canvas.coating.Clear();
                }
                this.rect = new Rectangle();
                pt = canvas.GetMousePosition();
                if (pt.x < 0)
                {
                    pt.x = 0;
                }
                else if (pt.x >= document.dw)
                {
                    pt.x = document.dw - 1;
                }
                if (pt.y < 0)
                {
                    pt.y = 0;
                }
                else if (pt.y >= document.dh)
                {
                    pt.y = document.dh - 1;
                }
                pr = canvas.GetRealMousePosition();
                if (pr.x < 0)
                {
                    pr.x = 0;
                }
                else if (pr.x >= canvas.coating.width)
                {
                    pr.x = canvas.coating.width - 1;
                }
                if (pr.y < 0)
                {
                    pr.y = 0;
                }
                else if (pr.y >= canvas.coating.height)
                {
                    pr.y = canvas.coating.height - 1;
                }
            }
            else
            {
                this.SelectionDown();
            }
            return;
        }// end function

        override public function MouseDrag(event:MouseEvent) : void
        {
            po = canvas.GetRealMousePosition();
            if (po.x < 0)
            {
                po.x = 0;
            }
            else if (po.x >= canvas.coating.width)
            {
                po.x = canvas.coating.width - 1;
            }
            if (po.y < 0)
            {
                po.y = 0;
            }
            else if (po.y >= canvas.coating.height)
            {
                po.y = canvas.coating.height - 1;
            }
            this.rect = Misc.CalculateRectangle(pr, po);
            switch(options.action)
            {
                case Action.RectangularMarqueeTool:
                {
                    canvas.coating.DrawRect(this.rect);
                    break;
                }
                case Action.EllipticalMarqueeTool:
                {
                    canvas.coating.DrawEllipse(this.rect);
                    break;
                }
                default:
                {
                    break;
                }
            }
            ed.navigator.SetSelectionSize(Math.round(this.rect.width / canvas.zoomlevel), Math.round(this.rect.height / canvas.zoomlevel));
            return;
        }// end function

        override public function MouseUp(event:MouseEvent) : void
        {
            ed.cursor.freeze = false;
            canvas.back.addEventListener(MouseEvent.MOUSE_MOVE, MouseMove, false, 0, true);
            ed.removeEventListener(MouseEvent.MOUSE_MOVE, this.MouseDrag, false);
            ed.removeEventListener(MouseEvent.MOUSE_UP, this.MouseUp, false);
            po = canvas.GetMousePosition();
            if (po.x < 0)
            {
                po.x = 0;
            }
            else if (po.x >= document.dw)
            {
                po.x = document.dw - 1;
            }
            if (po.y < 0)
            {
                po.y = 0;
            }
            else if (po.y >= document.dh)
            {
                po.y = document.dh - 1;
            }
            if (pt.x == po.x && pt.y == pt.y && !event.shiftKey && !event.ctrlKey)
            {
                if (selection != null)
                {
                    history.ChangePrepareType(Action.Deselect);
                    canvas.RemoveSelection();
                    history.Commit();
                }
            }
            else
            {
                if (selection == null)
                {
                    canvas.AddSelection();
                    selection = canvas.selection;
                }
                else if (!event.ctrlKey && !event.shiftKey)
                {
                    selection.Reset();
                }
                canvas.coating.Clear();
                this.rect = Misc.CalculateRectangle(pt, po);
                switch(options.action)
                {
                    case Action.RectangularMarqueeTool:
                    {
                        selection.EditRectangle(this.rect, options.feather, Boolean(!event.shiftKey && event.ctrlKey));
                        break;
                    }
                    case Action.EllipticalMarqueeTool:
                    {
                        selection.EditEllipse(this.rect, options.feather, options.antialias, Boolean(!event.shiftKey && event.ctrlKey));
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                selection.CreateOutline();
                canvas.coating.Redraw();
                ed.navigator.SetSelectionSize(selection.width, selection.height);
                if (selection != null && (selection.width == 0 || selection.height == 0))
                {
                    canvas.RemoveSelection();
                }
                history.Commit();
            }
            if (!AppState.OnCanvas)
            {
                ed.cursor.Set(ToolType.None);
            }
            return;
        }// end function

    }
}
